var common = require('../../../utils/common.js');
//获取应用实例
var app = getApp()
var startTime = null
var endTime = null
Page({
  data:{
    recordList:[],
    totalAmount:{},
    clickId:""//用于长按点击时，修改单项的颜色
  },
  onLoad: function () {
    // 页面首次渲染完毕时执行

  },
  onShow:function(options){
    var that = this
    // var appUsed = wx.getStorageSync('appused')
    // if (!appUsed) {
    //     // 跳转到app使用引导页面
    //     wx.navigateTo({
    //       url: '/pages/smartlife/guide/guide'
    //     })
    //     wx.setStorageSync('appused', true)
    // }
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration:10000
    })
    if(app.globalData.wangcaiToken){

    // if (wx.getStorageSync("wangcai_token")){
      // console.log(app.globalData.wangcaiToken)
      console.log(wx.getStorageSync("wangcai_token"))
      console.log("直接获取")
      that.getData();
    }else{
      console.log("间接获取")
      //调用应用实例的方法获取全局数据
      app.getToken("aaa",function (wangcaiToken) {
        console.log(wangcaiToken)
        //更新数据
        that.getData();
      })
    }
    
  },
  getData: function() {
    // console.log("wangcaiToken------"+app.globalData.wangcaiToken)
    console.log("getData")
    var that = this;
    wx.request({
      url: common.apiUrl()+'totalAmount',
      data: {
        token:app.globalData.wangcaiToken
      },
      success: function(res){
        console.log(res)
        if(res.data.status==1){
          that.setData({
            totalAmount:res.data.data
          })
          wx.request({
            url: common.apiUrl()+'detailOfThisMonth',
            data: {
              token:app.globalData.wangcaiToken
            },
            success: function(res){
              console.log(res)
              if(res.data.status==1){
                that.setData({
                  recordList:res.data.data
                })
              // }else{
              //   wx.showModal({
              //     title: '错误提示',
              //     content: res.data.msg,
              //     showCancel:false,
              //     success: function(res) {
              //       if (res.confirm) {
              //         app.getToken(function(wangcaiToken){
              //           //更新数据
              //           that.getData();
              //         })
              //       }
              //     }
              //   })
              }
              wx.hideToast()
            }
          })
        // }else{
        //   wx.showModal({
        //     title: '错误提示',
        //     content: res.data.msg,
        //     showCancel:false,
        //     success: function(res) {
        //       if (res.confirm) {
        //         app.getToken(function(wangcaiToken){
        //           //更新数据
        //           that.getData();
        //         })
        //       }
        //     }
        //   })
          // wx.hideToast()
        }
      }
    })
  },
  onShareAppMessage: function () {
    return {
      title: '旺财小程序记账，记账新时代，更安全、更方便、更快捷、更强大',
      path: '/pages/smartlife/wangcai/wangcai'
    }
  },
  totalRecord:function(e){
    wx.navigateTo({
      url: '/pages/smartlife/wangcai/totalrecord/totalrecord?type='+e.currentTarget.dataset.type+'&month='+e.currentTarget.dataset.month
    })
  },
  addRecord:function(){
    wx.navigateTo({
      url: '/pages/smartlife/wangcai/addrecord/addrecord'
    })
  },
  settingClick:function(){
    wx.navigateTo({
      url: '/pages/smartlife/wangcai/setting/setting'
    })
  },
  deleteRecord:function(e){
    var that = this
    that.setData({
      clickId:e.currentTarget.dataset.id
    })

    wx.showActionSheet({
      itemList: ['删除'],
      itemColor: '#E64340',
      success: function(res) {
        switch(res.tapIndex){
          case 0:
            // kind＝1，支出，kind＝2，收入
            var kind = "expense"
            if(e.currentTarget.dataset.kind==2){
              kind = "income"
            }
            wx.request({
              url: common.hostUrl()+ kind +'/delete',
              data: {
                ids:e.currentTarget.dataset.id
              },
              method:'POST',
              header: {
              'content-type': 'application/x-www-form-urlencoded'
                },
              success: function(res){
                that.setData({
                  clickId:""
                })
                if(res.data.status==1){
                  wx.showToast({
                    title: '删除成功',
                    icon: 'success',
                    success: function(e){
                      // 刷新数据
                      that.getData()
                    }
                  })
                }else{
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'success'
                  })
                }
              }
            })
          break;
          case 1:

          break;
        }
      },
      fail: function(res) {
        that.setData({
          clickId:""
        })
      }
    })
  },
  recordDetail: function(e) {
    if(this.endTime  - this.startTime < 350) {
        wx.navigateTo({
          url: '/pages/smartlife/wangcai/addrecord/addrecord?id='+e.currentTarget.dataset.id+'&kind='+e.currentTarget.dataset.kind
        })
    }
  },
  bindTouchStart: function(e) {
    this.startTime = e.timeStamp;
  },
  bindTouchEnd: function(e) {
    this.endTime = e.timeStamp;
  }
})